<html>
<head><meta charset="utf-8"><title>How to use read in packed_simd? · project-portable-simd · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/index.html">project-portable-simd</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html">How to use read in packed_simd?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234779873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234779873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234779873">(Apr 16 2021 at 01:17)</a>:</h4>
<p>Hey! First time Zulip user and ofc I'm also new to the Rust server (?) so let me know if I'm doing anything wrong. I have a question about <code>packed_simd</code> which I guess is owned by this working group now?</p>
<p>I'm stuck on trying to get a _gather_ instruction generated. I think I need to use something like  <a href="https://docs.rs/packed_simd/0.3.3/packed_simd/type.cptrx4.html#method.read"><code>cptrx4::read</code></a> for that. This is my current setup: <a href="https://gist.github.com/KarelPeeters/9b1fb30228258950e2452eb732ff5a91">https://gist.github.com/KarelPeeters/9b1fb30228258950e2452eb732ff5a91</a>. I can't figure out how to get the pointer array itself setup though, conceptally I just want <code>cptrx4::splat(vec.as_ptr()) + indices</code>. I guess I need some combination of <code>.to_bits()</code>/<code>.cast()</code>/<code>std::mem::transmute()</code> but I'm not sure what exactly and which of those are safe to use for pointers.</p>
<p>Does anyone know how to get there? Thanks!</p>



<a name="234781336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234781336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234781336">(Apr 16 2021 at 01:39)</a>:</h4>
<p>No, that's pretty much what you do:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="nc">cptrx4</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">cptrx4</span>::<span class="n">splat</span><span class="p">(</span><span class="n">vec</span><span class="p">.</span><span class="n">as_ptr</span><span class="p">()).</span><span class="n">add</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="234781580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234781580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234781580">(Apr 16 2021 at 01:42)</a>:</h4>
<p>Wait that just works? Haha that's a lot simpler than what I thought. I was trying to convert back and forth to integers because <code>+</code> didn't work but it's just <code>add</code> <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span> . Thanks a lot!</p>



<a name="234781667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234781667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234781667">(Apr 16 2021 at 01:44)</a>:</h4>
<p>A related question, why is there no version of <code>read</code> without the mask and extra values? That seems so odd to me, is it really that common to only load some values?</p>



<a name="234782071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234782071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234782071">(Apr 16 2021 at 01:50)</a>:</h4>
<p>Yes, there's some sort of masking going on for most vector gathers, e.g. <a href="https://www.felixcloutier.com/x86/vgatherdpd:vgatherqpd">https://www.felixcloutier.com/x86/vgatherdpd:vgatherqpd</a></p>



<a name="234782296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234782296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234782296">(Apr 16 2021 at 01:54)</a>:</h4>
<p>Ah interesting, so the target register is also reused as the default value register. This simd code is kind of funny, you're almost writing instructions but you still get register allocation from the compiler :)</p>



<a name="234782438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234782438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234782438">(Apr 16 2021 at 01:56)</a>:</h4>
<p>Yeah, <code>value</code> (the 3rd operand) is effectively the register you're clobbering, I suppose.</p>



<a name="234782529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234782529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234782529">(Apr 16 2021 at 01:58)</a>:</h4>
<p>Also this (packed_simd) is still the best way to do simd right now in Rust right?</p>



<a name="234782724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234782724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234782724">(Apr 16 2021 at 02:01)</a>:</h4>
<p>It is not particularly different from most code of the more numeric sort: There are many small differences between the vector operations supported by most ISAs. Likewise for the mathematical operations for most ISAs.</p>
<p>I usually recommend people look at <code>wide</code>, because it's supported on stable. Also, due to a slightly awkward series of events <code>packed_simd</code> is currently at <code>packed_simd_2</code> for bugfix releases.</p>



<a name="234783402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234783402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234783402">(Apr 16 2021 at 02:09)</a>:</h4>
<p>Hmm <code>wide</code> looks very similar to <code>packed_simd</code> but it looks like it's missing scatter and gather operations?<br>
I found out about <code>packed_simd_2</code> fortunately!</p>



<a name="234783517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234783517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234783517">(Apr 16 2021 at 02:11)</a>:</h4>
<p>And actaully what caues <code>packed_simd</code> to not work on stable yet?</p>



<a name="234783989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234783989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234783989">(Apr 16 2021 at 02:18)</a>:</h4>
<p>It relies on a ton of compiler intrinsics, which are inherently unstable (they could change at any time if necessary).  <code>packed_simd</code> itself will never work on stable, but our goal is to create <code>std::simd</code> which will be very similar</p>



<a name="234784475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234784475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234784475">(Apr 16 2021 at 02:25)</a>:</h4>
<p>The Rust compiler uses a ton of unstable features internally. <code>packed_simd</code> was... kinda tacitly designed around the assumption that it would become part of the Rust compiler, or a large part of it would, because it was part of a proposal to make it part of the standard library.</p>



<a name="234784543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234784543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234784543">(Apr 16 2021 at 02:26)</a>:</h4>
<p>And how does <code>wide</code> get away with running on stable then?</p>



<a name="234784854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234784854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234784854">(Apr 16 2021 at 02:31)</a>:</h4>
<p><code>wide</code> uses explicit vendor intrinsics, which are currently stable but aren't particularly portable.  The big difference is when doing runtime cpu feature detection, not compile time</p>



<a name="234784856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234784856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234784856">(Apr 16 2021 at 02:31)</a>:</h4>
<p>It is an abstraction over stabilized <code>std::arch</code> intrinsics.</p>



<a name="234785804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/How%20to%20use%20read%20in%20packed_simd%3F/near/234785804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Karel Peeters <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/How.20to.20use.20read.20in.20packed_simd.3F.html#234785804">(Apr 16 2021 at 02:47)</a>:</h4>
<p>Ah okay interesting, thanks for the answers!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>